package de.chludwig.ehumanities.provenance.utils;

import java.util.GregorianCalendar;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class CalendarUtil {
  
  private static final CalendarUtil instance = new CalendarUtil();
  
  public static CalendarUtil getInstance() {
    return instance;
  }
  
  /**
   * Get the current date and time.
   * @return a calendar that represents the current date and time in the default timezone.
   */
  public XMLGregorianCalendar currentTimestamp() {
    XMLGregorianCalendar timestamp;
    try {
      DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
      timestamp = datatypeFactory.newXMLGregorianCalendar(new GregorianCalendar());
    }
    catch (DatatypeConfigurationException e) {
      throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    }
    return timestamp;
  }

}
